{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4febc98a",
   "metadata": {
    "papermill": {
     "duration": 0.017637,
     "end_time": "2022-04-21T09:32:19.905791",
     "exception": false,
     "start_time": "2022-04-21T09:32:19.888154",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# 机器读心术之神经网络与深度学习第6课书面作业\n",
    "学号：207567\n",
    "\n",
    "**书面作业：**  \n",
    "编程实现RBM的CD快速学习算法（在张春霞的综述论文里提供了伪代码），可以使用任何编程语言，然后用一实例进行验证训练，抓图测试过程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "451aa6fd",
   "metadata": {
    "papermill": {
     "duration": 0.01594,
     "end_time": "2022-04-21T09:32:19.939316",
     "exception": false,
     "start_time": "2022-04-21T09:32:19.923376",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "## 作业内容\n",
    "用python基于Kaggle环境实现一个数字识别系统：\n",
    "1. 训练集用印刷体0~9数字；\n",
    "2. 构建RBM模型, 实现CD算法；\n",
    "3. 用CD算法训练RBM模型；\n",
    "4. 用MNIST数据集手写数字来测试模型。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3f3bee08",
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "execution": {
     "iopub.execute_input": "2022-04-21T09:32:19.973653Z",
     "iopub.status.busy": "2022-04-21T09:32:19.973289Z",
     "iopub.status.idle": "2022-04-21T09:32:20.206173Z",
     "shell.execute_reply": "2022-04-21T09:32:20.205296Z"
    },
    "papermill": {
     "duration": 0.253629,
     "end_time": "2022-04-21T09:32:20.209225",
     "exception": false,
     "start_time": "2022-04-21T09:32:19.955596",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import numpy as np # linear algebra\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06ec82d0",
   "metadata": {
    "papermill": {
     "duration": 0.017955,
     "end_time": "2022-04-21T09:32:20.243350",
     "exception": false,
     "start_time": "2022-04-21T09:32:20.225395",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### 构建印刷体数字训练集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "50b0f39d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:32:20.277260Z",
     "iopub.status.busy": "2022-04-21T09:32:20.276994Z",
     "iopub.status.idle": "2022-04-21T09:32:20.816849Z",
     "shell.execute_reply": "2022-04-21T09:32:20.815614Z"
    },
    "papermill": {
     "duration": 0.560713,
     "end_time": "2022-04-21T09:32:20.820245",
     "exception": false,
     "start_time": "2022-04-21T09:32:20.259532",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAAC+CAYAAACWL9wvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAzjElEQVR4nO3deXQUVdo/8G/1lu6EdDckJiRmIwtJSKIhHSERMYCyjRhFTVRUBuM4CjoKr/MefX8oir7yqkfEc2DQkRGdQQHDKAHC4jZEEQhMErJApEMIJCFsYcm+9XJ/fzgdbXrJVlXdHZ7POfUHXZ2qWw+3nr51q+pejjEGQggh4pC4ugCEEHI9oaRLCCEioqRLCCEioqRLCCEioqRLCCEikjlbyXHcdfFoA2OM6+93KSb2UVxsUUxsUUyopUsIIaKipEsIISJy2r0gNLlcDq1WC4nEee43m81obm5GT0+PSCVzH1KpFFqtFjLZL/9VZrMZTU1NMBgMLi6ZuK7nuqJSqeDr6wuOG1CPz4C0tbWhvb1dsO27C4lEAo1GA4VCAQBgjKG5uRnd3d2ilcGlSTchIQFvvPEGtFqt0++1tbXh1Vdf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IybXnzbPPPtvnleJA9PT0sMcff1yUeiJIS9fyi6HX65Gfn2/TfxIYGIjx48f3+5fJaDSipKTEZmoexhj0ev2QBwB3BUtr7fTp09i1a5fN4MlarRapqakO79gPx5j81vnz5/Gvf/0LoaGhGD9+PFpaWlBcXIxjx4655eDafOL7/LEwm80oLS3F2bNnbdZVVlZ6RJ2xnDfV1dXYuXNn79Vgf2PS09ODoqIim3slJpMJZ86cEazcVoT8VVIoFEytVtss2dnZA5ocrrm5mc2dO9futhQKhUe0Xhwtcrmc+fr62hxXRkYGq6+v94iYCBkXS13Zt28fi4iIYD4+Prz0uXlCXeHr/LHo6Ohg8+fPt7tNLy8vj4jJb+vHYGJy4cIFNnPmTLsxkMvlotQTQZ9e6OnpsfvcXF1dHfbt22d3Kmh7Wltb0dDQgJaWFr6L6HKOBkk+d+4cDh48iICAALt/N5xjAvwaF0tdqaqq8uhp1QeDr/PHoru7G7W1tcOizlx73vQ3JleuXHH5ecMxJ53m/2lR8E6hUECtVvf70RXGGFpaWniZqsTB9vv9DI1QMbmWTCaDRqNxOPauO8UEEL6uGI1GNDc3u/wmjzvUlYGePxaMMbS2tg56fA8n2/WYmJjNZrS0tPA+G8S1nMXEJUnX3bhDpXE37pJ03Q3VFVsUE1vOYkJTGBBCiIgo6RJCiIicdi8QQgjhF7V0CSFERJR0CSFERJR0CSFERJR0CSFERJR0CSFERJR0CSFERP8frLvWKvAVZykAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 10 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "trainX = np.zeros((10,28,28))\n",
    "for i in range(10):\n",
    "    cv2.putText(trainX[i], str(i),(4,24), cv2.FONT_HERSHEY_SIMPLEX, 1, (1, 1, 1), 2)\n",
    "    plt.subplot(2,5,i+1)\n",
    "    plt.imshow(trainX[i],cmap='gray'), plt.axis('off')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec297d8e",
   "metadata": {
    "papermill": {
     "duration": 0.017251,
     "end_time": "2022-04-21T09:32:20.855426",
     "exception": false,
     "start_time": "2022-04-21T09:32:20.838175",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "下面是测试数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a1c6aa12",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:32:20.892109Z",
     "iopub.status.busy": "2022-04-21T09:32:20.891850Z",
     "iopub.status.idle": "2022-04-21T09:32:21.370416Z",
     "shell.execute_reply": "2022-04-21T09:32:21.369244Z"
    },
    "papermill": {
     "duration": 0.500014,
     "end_time": "2022-04-21T09:32:21.373000",
     "exception": false,
     "start_time": "2022-04-21T09:32:20.872986",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_ds = np.load('../input/mnist-numpy-bxcxhxw-bxhxwxc/test_1_28_28.npy')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38581240",
   "metadata": {
    "papermill": {
     "duration": 0.016746,
     "end_time": "2022-04-21T09:32:21.407263",
     "exception": false,
     "start_time": "2022-04-21T09:32:21.390517",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### 构建RBM模型，实现CD算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "28aeacf3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:32:21.442814Z",
     "iopub.status.busy": "2022-04-21T09:32:21.442556Z",
     "iopub.status.idle": "2022-04-21T09:32:21.460772Z",
     "shell.execute_reply": "2022-04-21T09:32:21.459998Z"
    },
    "papermill": {
     "duration": 0.03868,
     "end_time": "2022-04-21T09:32:21.463061",
     "exception": false,
     "start_time": "2022-04-21T09:32:21.424381",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "        return 1/(1+np.exp(0-x))\n",
    "    \n",
    "class RBM:\n",
    "    def __init__(self, input_units=10, hidden_units=5, lr=0.001, T=100, rho=0.5):\n",
    "        self.W = np.random.rand(input_units, hidden_units)\n",
    "        self.a = np.random.rand(input_units)\n",
    "        self.b = np.random.rand(hidden_units)\n",
    "        self.v = (np.random.rand(input_units)>=rho).astype(float)\n",
    "        self.h = (np.random.rand(hidden_units)>=rho).astype(float)\n",
    "        self.input_units = input_units\n",
    "        self.hidden_units = hidden_units\n",
    "        self.lr = lr\n",
    "        self.T = T\n",
    "        self.rho = rho\n",
    "        \n",
    "    def sample(self, probs):\n",
    "        sa = np.random.rand(probs.shape[0])\n",
    "        return (probs>=sa).astype(np.uint8)\n",
    "        \n",
    "    def train(self, x):\n",
    "        v1 = x\n",
    "        rho = self.rho\n",
    "        \n",
    "        for i in range(self.T):\n",
    "            ph1 = sigmoid(np.matmul(v1, self.W)+self.b)\n",
    "            h1 = self.sample(ph1)\n",
    "            \n",
    "            v2 = self.sample(sigmoid(np.matmul(self.W, h1)+self.a))\n",
    "            ph2 = sigmoid(np.matmul(v2, self.W)+self.b)\n",
    "            \n",
    "            self.W += self.lr*(np.matmul(v1.reshape(self.input_units,1), \n",
    "                                         ph1.reshape(1,self.hidden_units))- \n",
    "                               np.matmul(v2.reshape(self.input_units,1), \n",
    "                                         ph2.reshape(1,self.hidden_units)))\n",
    "            self.a += self.lr*(v1-v2)\n",
    "            self.b += self.lr*(ph1-ph2)\n",
    "        \n",
    "        out = self.forward(x)\n",
    "        return np.linalg.norm(x-out)\n",
    "\n",
    "    def forward(self, x, k=1):\n",
    "        v1 = x\n",
    "        for i in range(k):\n",
    "            h1 = self.sample(sigmoid(np.matmul(v1, self.W)+self.b)>=self.rho)\n",
    "            v1 = self.sample(sigmoid(np.matmul(self.W, h1)+self.a)>=self.rho)\n",
    "        return v1\n",
    "    \n",
    "    def __call__(self, x, k=1):\n",
    "        return self.forward(x, k)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c2c3a93",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T08:57:36.548407Z",
     "iopub.status.busy": "2022-04-21T08:57:36.548089Z",
     "iopub.status.idle": "2022-04-21T08:57:36.556223Z",
     "shell.execute_reply": "2022-04-21T08:57:36.55532Z",
     "shell.execute_reply.started": "2022-04-21T08:57:36.548361Z"
    },
    "papermill": {
     "duration": 0.016702,
     "end_time": "2022-04-21T09:32:21.497760",
     "exception": false,
     "start_time": "2022-04-21T09:32:21.481058",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### 用CD算法训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "773344c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:32:21.534168Z",
     "iopub.status.busy": "2022-04-21T09:32:21.533295Z",
     "iopub.status.idle": "2022-04-21T09:32:58.894101Z",
     "shell.execute_reply": "2022-04-21T09:32:58.892274Z"
    },
    "papermill": {
     "duration": 37.383751,
     "end_time": "2022-04-21T09:32:58.898496",
     "exception": false,
     "start_time": "2022-04-21T09:32:21.514745",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Epoch 0: 100%|██████████| 10/10 [00:03<00:00,  2.67batch/s, loss=16.358]\n",
      "Epoch 1: 100%|██████████| 10/10 [00:03<00:00,  2.73batch/s, loss=2.680]\n",
      "Epoch 2: 100%|██████████| 10/10 [00:03<00:00,  2.66batch/s, loss=0.847]\n",
      "Epoch 3: 100%|██████████| 10/10 [00:03<00:00,  2.72batch/s, loss=0.606]\n",
      "Epoch 4: 100%|██████████| 10/10 [00:03<00:00,  2.83batch/s, loss=0.373]\n",
      "Epoch 5: 100%|██████████| 10/10 [00:03<00:00,  2.73batch/s, loss=0.000]\n",
      "Epoch 6: 100%|██████████| 10/10 [00:03<00:00,  2.78batch/s, loss=0.000]\n",
      "Epoch 7: 100%|██████████| 10/10 [00:04<00:00,  2.38batch/s, loss=0.000]\n",
      "Epoch 8: 100%|██████████| 10/10 [00:03<00:00,  2.65batch/s, loss=0.000]\n",
      "Epoch 9: 100%|██████████| 10/10 [00:03<00:00,  2.74batch/s, loss=0.000]\n"
     ]
    }
   ],
   "source": [
    "rbm = RBM(784,256)\n",
    "epochs = 10\n",
    "for epoch in range(epochs):\n",
    "    err = 0.0\n",
    "    with tqdm(range(trainX.shape[0]), unit=\"batch\") as tepoch: \n",
    "        for i in tepoch:\n",
    "            tepoch.set_description(f\"Epoch {epoch}\") \n",
    "            sample = trainX[i].reshape(784)\n",
    "            err += rbm.train(sample)\n",
    "            tepoch.set_postfix(loss='{:.3f}'.format(err/(i+1))) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b10c88b",
   "metadata": {
    "papermill": {
     "duration": 0.136063,
     "end_time": "2022-04-21T09:32:59.220880",
     "exception": false,
     "start_time": "2022-04-21T09:32:59.084817",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "### 用MNIST数据集手写数字来测试模型\n",
    "在测试集中随机选择一张手写数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f574fff7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:32:59.491909Z",
     "iopub.status.busy": "2022-04-21T09:32:59.491627Z",
     "iopub.status.idle": "2022-04-21T09:32:59.692827Z",
     "shell.execute_reply": "2022-04-21T09:32:59.691879Z"
    },
    "papermill": {
     "duration": 0.33935,
     "end_time": "2022-04-21T09:32:59.695099",
     "exception": false,
     "start_time": "2022-04-21T09:32:59.355749",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f8db324e910>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMA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E/ykNV9eU1qe0hrnIa74lq22ma8c8z47Fb5vTni5jjzH4H8EZV/bKqfgN8Hzg2Qx1rr6qeBS5dsfgYcGJ4fYLNfywrt0Nta6GqzlfVC8Prd4DL04zPeux2qWsl5gj7UeBXW96fZb3mey/gR0meT7IxdzHbOLxlmq23gMNzFrONPafxXqUrphlfm2O3yPTnYzlA90F3VtWfAn8B/NXQXF1LtdkHW6drp/8I/AmbcwCeB/5+zmKGacYfA/6mqv5362dzHrtt6lrJcZsj7OeAm7e8/9iwbC1U1bnh+SLwBJvdjnVy4fIMusPzxZnreV9VXaiqd6vqPeDbzHjshmnGHwO+V1WPD4tnP3bb1bWq4zZH2J8Dbk3yiSQfAT4HnJyhjg9Icv0wcEKS64HPsH5TUZ8Ejg+vjwNPzljL71iXabx3mmacmY/d7NOfV9XKH8A9bI7I/xfw1Tlq2KGuPwb+c3i8MndtwKNsNuv+j82xjb8E/gB4Bngd+A/gxjWq7Z/ZnNr7JTaDdWSm2u5ks4n+EvDi8Lhn7mO3S10rOW5+XVZqwgE6qQnDLjVh2KUmDLvUhGGXmjDsUhOGXWri/wEWUs3VK5FeIgAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "test_sample = (test_ds[425][0]>127).astype(np.uint8).reshape(784)\n",
    "plt.imshow(test_sample.reshape(28,28),cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55a69eec",
   "metadata": {
    "papermill": {
     "duration": 0.13744,
     "end_time": "2022-04-21T09:32:59.970607",
     "exception": false,
     "start_time": "2022-04-21T09:32:59.833167",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "只迭代一次的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "49e67ba6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:33:00.248422Z",
     "iopub.status.busy": "2022-04-21T09:33:00.248140Z",
     "iopub.status.idle": "2022-04-21T09:33:00.458934Z",
     "shell.execute_reply": "2022-04-21T09:33:00.457917Z"
    },
    "papermill": {
     "duration": 0.352419,
     "end_time": "2022-04-21T09:33:00.461119",
     "exception": false,
     "start_time": "2022-04-21T09:33:00.108700",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f8db2963390>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAALxElEQVR4nO3dT6hc5R3G8eepfzYqNKn0comxscWdCy2SVSh2oaTZRDeiq4iF66IWuzO0CwURpLR2WYgYTItVBGMNUqqpiHEluQk25g+aVCLmcs0lpKVxZTW/LubcMMaZO5M5f2d+3w8MM3Pu3Dm/nOTJ+573nTOvI0IAZt932i4AQDMIO5AEYQeSIOxAEoQdSOLqJndmm6F/oGYR4UHbS7Xstrfa/sj2Kds7y7wXgHp50nl221dJ+ljS3ZLOSDoo6cGIOL7G79CyAzWro2XfLOlURHwSEV9KelnS9hLvB6BGZcK+QdJnfc/PFNu+wfaC7UXbiyX2BaCk2gfoImKXpF0S3XigTWVa9iVJG/ue31RsA9BBZcJ+UNKttm+xfa2kByTtq6YsAFWbuBsfEV/ZflTSm5KukrQ7Io5VVhmASk089TbRzjhnB2pXy4dqAEwPwg4kQdiBJAg7kARhB5Ig7EASjV7PjuaNmlq1B87SNKJsbV3+s3URLTuQBGEHkiDsQBKEHUiCsANJEHYgCabeZlzd0091XjXJ1Fq1aNmBJAg7kARhB5Ig7EAShB1IgrADSRB2IAnm2RvAfDG6gJYdSIKwA0kQ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WW8hy0zrpaPXevLn0dE4zdJ29Qbkf+XpN+0UcOQun4o6Z/F7VjbtUl6Sb1u3f/UG9v4uaTvSXpb0klJ/5C0vkO1/Vm9pb2PqBes+ZZq26JeF/2IpA+K27a2j90adTVy3Pi4LJAEA3RAEoQdSIKwA0kQdiAJwg4kQdiBJAg7kMT/AeGTXjBCI4jhAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "result = rbm(test_sample)\n",
    "plt.imshow(result.reshape(28,28),cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7b524de",
   "metadata": {
    "papermill": {
     "duration": 0.137305,
     "end_time": "2022-04-21T09:33:00.735824",
     "exception": false,
     "start_time": "2022-04-21T09:33:00.598519",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "迭代10次到达热平衡后："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "068443a7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-21T09:33:01.013557Z",
     "iopub.status.busy": "2022-04-21T09:33:01.013262Z",
     "iopub.status.idle": "2022-04-21T09:33:01.216812Z",
     "shell.execute_reply": "2022-04-21T09:33:01.215818Z"
    },
    "papermill": {
     "duration": 0.346431,
     "end_time": "2022-04-21T09:33:01.219049",
     "exception": false,
     "start_time": "2022-04-21T09:33:00.872618",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x7f8db28e83d0>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9A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+7tLRWkq8DF6vqa8MfygNV9YUNqe1hrnIZ7xXVNmmZ8c8w4rFb5vLn8xijZb8TeKOqflFVvwO+BxwZoY6NV1XPAhev2HwEOD7cPs72f5a1m1DbRqiqc1X1wnD7HeDSMuOjHrs96lqLMcJ+GPjljvtn2Kz13gv4YZLnk2yNXcwuDu5YZust4OCYxexi6jLe63TFMuMbc+zmWf58UQ7Qvd9dVfXXwN8B/zB0VzdSbZ+DbdLc6b8Cf8X2GoDngH8Zs5hhmfHHgH+qqt/sfGzMY7dLXWs5bmOE/Sxwy477Hx62bYSqOjtcXwCeYPu0Y5Ocv7SC7nB9YeR6Lquq81X1blW9B3yLEY/dsMz4Y8B3q+rxYfPox263utZ13MYI+3PAbUk+muSDwKeBEyPU8T5JbhgGTkhyA/BJNm8p6hPA0eH2UeDJEWv5I5uyjPekZcYZ+diNvvx5Va39AtzL9oj8/wFfGqOGCXX9JfC/w+WVsWsDHmW7W/f/bI9t/D3wZ8AzwOvA/wA3bVBt/8720t4vsR2sQyPVdhfbXfSXgBeHy71jH7s96lrLcfPjslITDtBJTRh2qQnDLjVh2KUmDLvUhGGXmjDsUhN/AN5w3/YDRORnAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "result = rbm(test_sample, 10)\n",
    "plt.imshow(result.reshape(28,28),cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1ed4ef7",
   "metadata": {
    "papermill": {
     "duration": 0.14002,
     "end_time": "2022-04-21T09:33:01.498794",
     "exception": false,
     "start_time": "2022-04-21T09:33:01.358774",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "从测试结果看还可以，但是也有错误的时候。  \n",
    "截图链接：![mind06-1](https://gitee.com/dotzhen/cloud-notes/raw/master/mind06-1.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8ffe48a5",
   "metadata": {
    "papermill": {
     "duration": 0.139283,
     "end_time": "2022-04-21T09:33:01.779259",
     "exception": false,
     "start_time": "2022-04-21T09:33:01.639976",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 53.480578,
   "end_time": "2022-04-21T09:33:02.842365",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2022-04-21T09:32:09.361787",
   "version": "2.3.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
